import _ from 'lodash';

export default {
  props: {
    collectProps: {
      type: Object,
      default: () => ({
        type: 'choice'
      })
    }
  },

  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.inputValue = val;
        // const index = _.findIndex(
        //   this.option,
        //   ({ field_value: fv, field_label: fl }) => fv === val || fl === val
        // );

        // if (index > -1) {
        //   const { field_label } = this.option[index];
        //   this.inputValue = field_label;
        // }
      }
    }
  },

  computed: {
    // 获取value
    infoValue() {
      const index = _.findIndex(
        this.option,
        ({ field_value: fv, field_label: fl }) => fv === this.value || fl === this.value
      );

      const { field_value = '' } = this.option[index] || {};

      return field_value;
    },

    // 信息收集
    collectInfo() {
      if (this.can_do) return null;
      const { type } = this.collectProps;

      return {
        field: this.id || this.value,
        field_value: {
          [type]: this.infoValue
        }
      };
    }
  }
};
